package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface UserInfoMapper {

    @Select("select * from userinfo")
    List<UserInfo>  selectAll();

    @ResultMap(value = "BaseMap")
    @Select("select * from userinfo where id=#{id}")
    UserInfo selectOnt(Integer id);

    // 使用原生mysql as 重命名
    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFiag, " +
            "create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo>  selectAll2();

    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })

    // 使用注解 进行重命名
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from userinfo")
    List<UserInfo>  selectAll3();

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values (#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);

    /**
     * 参数为对象时,如果对对象重命名, 就算是和重命名前的名字一样
     * 也要使用 重命名名字.对象属性
     * @param userInfo
     * @return
     */
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id=#{id}")
    Integer delete(Integer id);

    @Update("update userinfo set gender=#{gender} where id=#{id}")
    Integer update(UserInfo userInfo);

}
